
#include <iostream>

#include <tr1/unordered_map>
#include <tr1/unordered_set>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/join.hpp>

#include "variable_manager.hpp"

using namespace boost;

logical_variable_t variable_manager_t::register_variable( const string &var ) {
  logical_variable_t  v_hash = std::tr1::hash<string>()( var );
  vs_dict_t::iterator iter_v = m_p2v.find( v_hash );
  if( m_p2v.end() == iter_v )      m_p2v[ v_hash ] = var;
  else if( var != iter_v->second ) cerr << var << " collides " << iter_v->second << "!" << endl;                                       
  return v_hash;
}

string variable_manager_t::p2v( const logical_variable_t &p ) const {
  vs_dict_t::const_iterator iter_v = m_p2v.find( p );
  return m_p2v.end() == iter_v ? "?" : iter_v->second;
}
    
string variable_manager_t::join( const logvar_list_t& il, const string& delimiter ) const {
  vector<string> sl;
  for( int i=0; i<il.size(); i++ ) sl.push_back( p2v( il[i] ) );
  return ::join( sl, delimiter );
}
  
string variable_manager_t::join( const logvar_set_t& il, const string& delimiter ) const {
  vector<string> sl;
  for( logvar_set_t::const_iterator iter=il.begin(); il.end()!=iter; ++iter ) sl.push_back( p2v( *iter ) );
  return ::join( sl, delimiter );
  }
